﻿using System.Configuration;
using cn.jpush.api;
using cn.jpush.api.common;
using cn.jpush.api.push;
using cn.jpush.api.push.mode;
using cn.jpush.api.push.notification;
using QiBu.Api.Helper.Common;

namespace QiBu.Api.Helper.jPush
{
    public class PushHelper
    {
        /// <summary>
        /// 推送给指定对象数组以逗号分开
        /// </summary>
        /// <param name="type">自定义推送对象：0.全部 1.ids(设备别名) 2.tags(设备标签)</param>
        /// <param name="ids">目标用户ID</param>
        /// <param name="alert">推送弹出提示</param>
        /// <param name="title">推送标题</param>
        /// <param name="msg">推送的消息</param>
        public static bool PushToUserIds(int type, string[] ids, string alert, string title, string msg, int msgType, int objId = 0)
        {
            PushPayload pushPayload = new PushPayload();
            pushPayload.platform = Platform.all();
            Audience audience;
            if (type == 1)
            {
                audience = Audience.s_alias(ids);//推送给用户列表（设备别名）
            }
            else if (type == 2)
            {
                audience = Audience.s_tag(ids);//推送给用户列表（设备标签）
            }
            else
            {
                audience = Audience.all();//推送给全部对象
            }
            pushPayload.audience = audience;
            var notification = new Notification().setAlert(alert);
            notification.AndroidNotification = new AndroidNotification().setTitle(title);
            //Android
            notification.AndroidNotification.AddExtra("msgType", msgType);
            notification.AndroidNotification.AddExtra("Id", objId);
            //Apple
            notification.IosNotification = new IosNotification();
            notification.IosNotification.incrBadge(1);
            notification.IosNotification.setSound("default");
            notification.IosNotification.AddExtra("msgType", msgType);
            notification.IosNotification.AddExtra("Id", objId);
            
            pushPayload.notification = notification.Check();
            try
            {
                var res = PushMsg(pushPayload);
                return res.ResponseResult.responseCode.ToString() == "OK";
            }
            catch (APIRequestException e)
            {
                //ErrorCode备注
                //0:SUCCEED_RESULT_CODE
                //1002:LACK_OF_PARAMS
                //1003:INVALID_PARAMS
                //1004:AUTHENTICATION_FAIL
                //1005:TOO_BIG
                //1008:APPKEY_NOT_EXIST
                //1011:NO_TARGET
                LogHelper.Info(e.ErrorCode + ":" + e.ErrorMessage);
                return false;
            }
        }

        /// <summary>
        /// 消息推送
        /// </summary>
        /// <param name="pushPayload"></param>
        public static MessageResult PushMsg(PushPayload pushPayload)
        {
            LogHelper.Info("推送:" + pushPayload);
            string app_key = ConfigurationManager.AppSettings["app_key"].Trim();
            string master_secret = ConfigurationManager.AppSettings["master_secret"].Trim();
            JPushClient client = new JPushClient(app_key, master_secret);
            try
            {
                return client.SendPush(pushPayload);
            }
            catch (APIRequestException e)
            {
                throw e;
            }
        }
    }
}
